package com.atlearn.boot_web.controller;

import com.atlearn.boot_web.bean.Person;
import com.atlearn.boot_web.util.AttendenceUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
public class TestConrtoller {
    @GetMapping("/test/{msg}")
    @ResponseBody
    public String test1(@PathVariable("msg") String msg){
        return msg;
    }

    @GetMapping(value = "/test2")
    public String test2(){
    return "forward:/success";
    }

    @GetMapping("/success")
    @ResponseBody
    public String success(){
    return "转发成功";
    }

    @GetMapping("/test3")
    @ResponseBody
    public String test3(@RequestParam("msg") String msg){
        return msg;
    }

    @GetMapping("/test4")
    public String test4(HttpServletRequest request){
     request.setAttribute("name","zxb");
     request.setAttribute("age",18);
     return "forward:/success2";
    }

    @GetMapping("/success2")
    @ResponseBody
    public String success2(@RequestAttribute("name") String name,@RequestAttribute("age") Integer age){
        HashMap map = new HashMap<String,Object>();
        map.put("name",name);
        map.put("age",18);
        map.put("msg","success2");
        return map.toString();
    }
   @GetMapping("/params")
    public String test5(Map<String,Object> map,
                      Model model,
                      HttpServletRequest request,
                      HttpServletResponse response){
                map.put("hello","world666");
                model.addAttribute("wolrd","hello666");
                request.setAttribute("msg","helloworld");
                Cookie cookie = new Cookie("c","v");
                cookie.setDomain("localhost");
                response.addCookie(cookie);
                return "forward:success3";
    }
    @GetMapping("/success3")
    @ResponseBody
    public String success3(@RequestAttribute("msg") String name,
                           HttpServletRequest request){
        HashMap map = new HashMap<String,Object>();
        Object msg = request.getAttribute("msg");
        Object hello = request.getAttribute("hello");
        Object world = request.getAttribute("wolrd");
        Cookie[] cookies = request.getCookies();
        map.put("msg",msg);
        map.put("hello",hello);
        map.put("world",world);
        map.put("cookies",cookies);
        return map.toString();
    }
    /*
    *关联保存表单
    * */
    @PostMapping("/saveuser")
    @ResponseBody
    public Person saveuser(Person person){
        return person;
    }
    @GetMapping("/testday")
    @ResponseBody
    public String test(){
        SimpleDateFormat sf=new SimpleDateFormat("yyyy");
        SimpleDateFormat sf2=new SimpleDateFormat("yyyy-MM");
        Date day=new Date();
        String year = sf.format(day);
        String month = sf2.format(day);
        String[] split = month.split("-");
        int m = Integer.parseInt(split[1]);
        Calendar cal = Calendar.getInstance();
        // 设置月份
        cal.set(Calendar.MONTH, m - 1);
        // 获取某月最大天数
        int lastDay=0;
        //2月的平年瑞年天数
        if(m==2) {
            lastDay = cal.getLeastMaximum(Calendar.DAY_OF_MONTH);
        }else {
            lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }

    @GetMapping("/attendence")
    @ResponseBody
    public String getatu(){
        AttendenceUtil atu = new AttendenceUtil();
        String url="https://k8sszyd.cloudcc.com/distributor.action";
        String getbinding = atu.getbinding(url, "zhengxb@cloudcc.com", "222222");
        String position="中国四川省成都市武侯区天晖路中航城市广场附近";
        //String ps = atu.getURLEncoderString(position);
        Random random = new Random();
        int  s1=random.nextInt(10);
        int  s2=random.nextInt(10);
        int  s3=random.nextInt(10);
        int  s4=random.nextInt(10);
        Double lng=104.0705+0.00001*s1+0.000001*s2;
        String n=lng+"";
        Double lat=30.6903+0.00001*s3+0.000001*s4;
        String a=lat+"";
        String data_opt="binding="+getbinding+"&lat="+a+"&lng="+n+"&position="+position+"&serviceName=checkout&type=1";
        String operate="";
        if(!getbinding.equals("")){
             operate = atu.operate(url,data_opt);
        }

        return operate;


    }

}
